package com.javasm.finance.service.impl;

import com.javasm.finance.dao.impl.ProdDaoImpl;
import com.javasm.finance.dao.ProdDao;
import com.javasm.finance.service.ProdService;
import com.javasm.finance.util.DBUtils;
import com.javasm.finance.entity.Product;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProdServiceImpl implements ProdService {
    ProdDao prodDao = new ProdDaoImpl();

    @Override
    public List<Product> findQueryAll(Integer page, Integer pageSize, String prodName) {
        List<Product> productList = null;
        try {
            productList = prodDao.findQueryAll(page, pageSize, prodName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return productList;
    }

    @Override
    public long findTotalNum(String prodName) {
        long totalNum = 0;
        try {
            totalNum = prodDao.findTotalNum(prodName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalNum;

    }

    @Override
    public Product findByProdId(Integer prodId) {
        Product product = null;
        try {
            product = prodDao.findByProdId(prodId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return product;
    }

    @Override
    public int edit(Product product) {
        int rows = 0;
        try {
            rows = prodDao.edit(product);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }

    @Override
    public int addProd(Product product) {
        int rows = 0;
        try {
            rows = prodDao.addProd(product);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }

    @Override
    public Product finQueryProdParent(String prodName) {
        Product product = null;
        try {
            product = prodDao.finQueryProdParent(prodName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return product;
    }

    @Override
    public List<Product> findQueryProd() {
        List<Product> list = null;
        try {
            list = prodDao.findQueryProd();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean delete(Integer prodId) {
        boolean isSuccess = false;
        Connection conn = DBUtils.getConn();
        try {
            conn.setAutoCommit(false);
            prodDao.delete(prodId, conn);
            org.apache.commons.dbutils.DbUtils.commitAndCloseQuietly(conn);
            isSuccess = true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        org.apache.commons.dbutils.DbUtils.rollbackAndCloseQuietly(conn);
        return isSuccess;
    }

    @Override
    public List<Product> findQueryRelevance(Integer parentId, Integer prodId) {
        List<Product> list = null;
        try {
            list = prodDao.findQueryRelevance(parentId, prodId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }


    @Override
    public boolean addRelevance(String relevance, Integer parentId, Integer prodId) {
        boolean result = false;
        Connection conn = DBUtils.getConn();
        try {
            conn.setAutoCommit(false);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            List<Product> relevance1 = prodDao.findQueryRelevance(parentId, prodId);

            int i = prodDao.addRelevance(relevance, prodId, conn);
            System.out.println(i);
            org.apache.commons.dbutils.DbUtils.commitAndCloseQuietly(conn);
            result = true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        org.apache.commons.dbutils.DbUtils.rollbackAndCloseQuietly(conn);


        return result;
    }

    @Override
    public List<Integer> findQueryRelevanceId(Integer prodId) {
        Product prodIdList = null;
        String relevance = null;
        String[] split = null;
        List<Integer> list = new ArrayList<>();
        try {
            prodIdList = prodDao.queryRelevanceId(prodId);
            relevance = prodIdList.getRelevance();
            split = relevance.split(",");
            for (int i = 0; i < split.length; i++) {
                list.add(Integer.valueOf(split[i]));
            }
            System.out.println("list" + list);
            System.out.println(Arrays.toString(split));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}




